package com.alliky.core.widget.rxdialog;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;

/**
 * @Author: wxianing
 * @CreateDate: 2022/7/31 21:30
 * @Description: java类作用描述
 */
public class RxBaseDialog extends AppCompatDialog {
    boolean cancelable = true;
    private boolean canceledOnTouchOutside = true;
    private boolean canceledOnTouchOutsideSet;
//    private RxSkinManager mSkinManager = null;

    public RxBaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

//    public void setSkinManager(@Nullable RxSkinManager skinManager) {
//        if(mSkinManager != null){
//            mSkinManager.unRegister(this);
//        }
//        mSkinManager = skinManager;
//        if(isShowing() && skinManager != null){
//            mSkinManager.register(this);
//        }
//    }

    @Override
    protected void onStart() {
        super.onStart();
//        if (mSkinManager != null) {
//            mSkinManager.register(this);
//        }
    }

    @NonNull
    @Override
    public LayoutInflater getLayoutInflater() {
        LayoutInflater layoutInflater = super.getLayoutInflater();
        LayoutInflater.Factory2 factory2 = layoutInflater.getFactory2();
//        if(factory2 instanceof QMUISkinLayoutInflaterFactory){
//            LayoutInflaterCompat.setFactory2(layoutInflater,
//                    ((QMUISkinLayoutInflaterFactory)factory2).cloneForLayoutInflaterIfNeeded(layoutInflater));
//        }
        return layoutInflater;
    }

    @Override
    protected void onStop() {
        super.onStop();
//        if (mSkinManager != null) {
//            mSkinManager.unRegister(this);
//        }
    }

    @Override
    public void setCancelable(boolean cancelable) {
        super.setCancelable(cancelable);
        if (this.cancelable != cancelable) {
            this.cancelable = cancelable;
            onSetCancelable(cancelable);
        }
    }

    protected void onSetCancelable(boolean cancelable) {

    }

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
        if (cancel && !cancelable) {
            cancelable = true;
        }
        canceledOnTouchOutside = cancel;
        canceledOnTouchOutsideSet = true;
    }

    protected boolean shouldWindowCloseOnTouchOutside() {
        if (!canceledOnTouchOutsideSet) {
            TypedArray a =
                    getContext()
                            .obtainStyledAttributes(new int[]{android.R.attr.windowCloseOnTouchOutside});
            canceledOnTouchOutside = a.getBoolean(0, true);
            a.recycle();
            canceledOnTouchOutsideSet = true;
        }
        return canceledOnTouchOutside;
    }

    @Override
    public void dismiss() {
        Context context = getContext();
        if (context instanceof ContextWrapper) {
            context = ((ContextWrapper) context).getBaseContext();
        }
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            if (activity.isDestroyed() || activity.isFinishing()) {
                return;
            }
            super.dismiss();
        } else {
            try {
                super.dismiss();
            } catch (Throwable ignore) {
            }
        }
    }
}
